package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BaseException;
import cn.wolfcode.car.common.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementMapper statementMapper;
    @Override
    public TablePageInfo< Appointment > query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }

    /**
     * 1创建时间是后台来完成添加的
     * 2日期无法封装 实体类贴注解@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
     * 3实际到店时间是不用填写的
     * @param appointment
     */
    @Override
    public void save(Appointment appointment) {
        Appointment newAppointment = new Appointment();
        newAppointment.setCreateTime(new Date());
        newAppointment.setCustomerName(appointment.getCustomerName());
        newAppointment.setCustomerPhone(appointment.getCustomerPhone());
        newAppointment.setAppointmentTime(appointment.getAppointmentTime());
        newAppointment.setLicensePlate(appointment.getLicensePlate());
        newAppointment.setCarSeries(appointment.getCarSeries());
        newAppointment.setServiceType(appointment.getServiceType());
        newAppointment.setInfo(appointment.getInfo());
        appointmentMapper.insert(newAppointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment dbApponitment = this.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(dbApponitment.getStatus())) {
            throw  new BusinessException("非法操作");
        }
        dbApponitment.setCustomerName(appointment.getCustomerName());
        dbApponitment.setCustomerPhone(appointment.getCustomerPhone());
        dbApponitment.setAppointmentTime(appointment.getAppointmentTime());
        dbApponitment.setLicensePlate(appointment.getLicensePlate());
        dbApponitment.setCarSeries(appointment.getCarSeries());
        dbApponitment.setServiceType(appointment.getServiceType());
        dbApponitment.setInfo(appointment.getInfo());
        appointmentMapper.updateByPrimaryKey(dbApponitment);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        //Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : ids) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    /**
     * 需要判断状态是否在预约中才能进行到店操作
     * 需要注意的客户到店的时候要修改到店时间
     *
     * @param id
     */
    @Override
    public void arrivalShop(Long id) {
        Appointment appointment = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw  new BusinessException("非法操作");
        }
        appointmentMapper.arrival(id,Appointment.STATUS_ARRIVAL);
    }
    /**
     * 需要判断状态是否在预约中才能进行到店操作
     *
     *
     * @param id
     */
    @Override
    public void cancelAppointment(Long id) {
        Appointment appointment = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw  new BusinessException("非法操作");
        }
        appointmentMapper.changeStatus(id,Appointment.STATUS_CANCEL);
    }

    /**
     * 1由于是由预约界面跳转过来的,所以为网上预约的账单
     *2首先需要根据ApponitmentId先查询出来statement判断是否为空(存在否结算单)
     *3存在直接返回StatementId让其他的页面完成账单明细的跳转
     *4不存在则将预约的值封装到Statement对象
     * 5最后修改Appointment对象为已结算的状态
     * 6返回statementId完成账单明细页面的跳转
     */
    @Override
    public Long generateStatement(Long id) {

        Appointment appointment = this.get(id);

        Statement statement = statementMapper.selectByApponitmentId(id);

        if (statement==null){
            statement = new Statement();
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setInfo(appointment.getInfo());
            statement.setCreateTime(new Date());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setServiceType(appointment.getServiceType());
            statement.setAppointmentId(id);
            statementMapper.insert(statement);
        }
        //修改为已结算状态
       appointmentMapper.changeStatus(id,Appointment.STATUS_SETTLE);
        return statement.getId();
    }

    @Override
    public void changeStatus(Integer statusArrival, Long appointmentId) {
        appointmentMapper.changeStatus(appointmentId,Appointment.STATUS_SETTLE);
    }


}
